package com.org.khtn.gui;

import java.awt.BorderLayout;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

public class QueryTab extends JSplitPane {
	public JTextArea queryContentArea;
	public JPanel resultPanel;
	public JTabbedPane resultTabbedPane;
	public JPanel resultTab;
	public JTextArea messageAreaTab;
	public JLabel notice;
	public String location;
	public static int number = 0;

	public QueryTab(Content_QueryContentPopupMenu queryContentPopupMenu,
			String path) {
		queryContentArea = new JTextArea();
		queryContentArea.setLineWrap(true);
		queryContentArea.setComponentPopupMenu(queryContentPopupMenu);
		if (path != "") {
			File f = new File(path);
			if (f.exists() == true) {
				try {
					FileInputStream fin = new FileInputStream(f);
					int[] b = new int[3];
					for (int i = 0; i < b.length; i++) {
						b[i] = fin.read();
					}
					String encoding = "ASCII";
					if (b[0] == 255 && b[1] == 254) {
						encoding = "UTF-16";
					} else if (b[0] == 254 && b[1] == 255) {
						encoding = "UTF-16BE";
					} else if (b[0] == 239 && b[1] == 187 && b[2] == 191) {
						encoding = "UTF-8";
					}

					queryContentArea.setText("");
					BufferedReader br = new BufferedReader(
							new InputStreamReader(new FileInputStream(f),
									encoding));
					br.skip(1);
					String line = br.readLine();
					while (line != null) {
						queryContentArea.append(line + "\n");
						line = br.readLine();
					}
					br.close();
					fin.close();
				}

				catch (Exception ex) {
				}
			}
		}

		resultPanel = new JPanel(new BorderLayout());

		resultTabbedPane = new JTabbedPane();

		resultTab = new JPanel(new BorderLayout());

		messageAreaTab = new JTextArea();
		messageAreaTab.setEditable(false);

		resultTabbedPane.addTab("Results", resultTab);
		resultTabbedPane.addTab("Messages", new JScrollPane(messageAreaTab));

		notice = new JLabel("Query executed successfully");

		resultPanel.add(resultTabbedPane, BorderLayout.CENTER);
		resultPanel.add(notice, BorderLayout.PAGE_END);
		resultPanel.setVisible(false);

		setOrientation(JSplitPane.VERTICAL_SPLIT);
		setLeftComponent(new JScrollPane(queryContentArea));
		setRightComponent(resultPanel);
		location = path;
	}
}
